package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.pojo.OrderDetail;
import com.hmall.common.search.OrderClient;
import com.hmall.item.conf.MqConstants;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    OrderClient orderClient;

    @GetMapping("list")
    public PageDTO<Item> list(@RequestParam("page") Integer page,
                              @RequestParam("size") Integer size){
//        System.out.println("分页查询"+page+","+size);
        return itemService.list(page, size);
    }


    /**
     * 根据id将商品状态上架或者下架
     * @param id 下架商品的id
     */
    @PutMapping("status/{id}/{status}")
    public void setStatus(@PathVariable(value = "id")Long id,
                        @PathVariable(value = "status")Integer status) {
        long a = id;
        long b = status;
        long[] arr = {a,b};

        rabbitTemplate.convertAndSend(MqConstants.Item_EXCHANGE,MqConstants.Item_INSERT_KEY,arr);

        itemService.upOrDown(id, status);
    }


    @DeleteMapping("{id}")
    public void delete(@PathVariable(value = "id")Long id){
        System.out.println("执行删除功能"+id);
        itemService.delete(id);
        rabbitTemplate.convertAndSend(MqConstants.Item_EXCHANGE,MqConstants.Item_DELETE_KEY,id);
        log.info("成功发送删除id功能,对应id{}",id);
    }

    @PostMapping()
    public void addItem(@RequestBody Item item ){
//        System.out.println("添加商品请求"+item);
        itemService.addItem(item);
    }


    @RequestMapping("{id}")
    public Item findItemById(@PathVariable(value = "id")Long id){
        return itemService.findItemById(id);
    }


    @PutMapping()
    public void update(@RequestBody Item item){
        itemService.updateById(item);

    }

    @GetMapping("/xx/{id}")
    public void updateItemNumById(@PathVariable(value = "id")Long orderId){

        System.out.println(orderId+"*******************");
        //根据订单号获取商品id跟购买数量
        OrderDetail orderDetail = orderClient.findOrderDetailById(orderId);
        //获取商品id
        Long orderIds = orderDetail.getItemId();
        //根据商品id获取剩余库存
        Item item1 = itemService.getById(orderIds);
        //获取商品库存
         Integer stock = item1.getStock();
        //获取购买数量
        Integer num = orderDetail.getNum();
        //得到剩余库存
        int s = stock-num;
         //减库
        UpdateWrapper<Item> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", orderIds).set("stock", s);
        itemService.update(wrapper);

    }
}
